local timePrevUpdate = system.getTimer()

local function makeImageScroller( texturePath )
	local imageScroller = { layer = display.newGroup(), imageList = {} }

	local image = display.newImage( imageScroller.layer, texturePath)
	imageScroller.staticPosX = imageScroller.layer.x
	imageScroller.staticPosY = imageScroller.layer.y
	local needWidth = display.contentWidth
	local needImageCount = math.ceil( needWidth / image.width )
	print( "needAmount : " .. needImageCount)
	print( "D width : " .. needWidth )
	print( "I width : " .. image.width )

	imageScroller.imageList[1] = image
	for i = 2, needImageCount + 1 do
		local nextImage = display.newImage( imageScroller.layer, texturePath)
		nextImage.x = ( image.width * i ) - ( image.width / 2 )
		imageScroller.imageList[i] = nextImage
	end
	print( #imageScroller.imageList)

	function imageScroller:enterFrame( event )
		local timeElapsed = event.time - timePrevUpdate
		local moveAmount = ( timeElapsed * 0.001 ) * 500
		print( "Move : " .. moveAmount )
		timePrevUpdate = event.time
		self.layer.x = self.layer.x - moveAmount

		local currentMoveDistance = math.abs( self.layer.x - self.staticPosX )
		if math.abs( self.layer.x - self.staticPosX ) > ( self.imageList[1].width ) then
			self.layer.x = self.layer.x + ( math.modf( currentMoveDistance / self.imageList[1].width ) * self.imageList[1].width )
		end
	end

	return imageScroller
end

function main()
	Runtime:addEventListener( "enterFrame", makeImageScroller( "bottomHalf.png" ) )
end

main()
